package org.woosiyuan.util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;

public class AppMain extends JFrame {

	private JButton btnMoveForward;
	private JButton btnMoveBack;
	private JButton btnMoveLeft;
	private JButton btnMoveRight;
	private JButton btnTurnLeft;
	private JButton btnTurnRight;
	
	private ArrayList<JButton> btns;
	
	private static Car car;
	public static void main(String args[]) {
		try {
			AppMain frame = new AppMain();
			frame.setVisible(true);
			
			ServerSocket server;
			Socket socket = null;
			try {
				server = new ServerSocket(9999);
				System.out.println("server listen...");
				socket = server.accept();
				car = new Car(socket);
				frame.setBtnEnable(true);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public AppMain() {
		super();
		setBounds(100, 100, 800, 575);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		{
			btnMoveForward = new JButton();
			btnMoveForward.setText("forward");
			btnMoveForward.setBounds(400, 100, 100, 30);
			btnMoveForward.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent arg0) {
					car.sendCommand().moveFoward();
				}
			});
			this.add(btnMoveForward);
		}
		{
			btnMoveBack = new JButton();
			btnMoveBack.setText("back");
			btnMoveBack.setBounds(400, 150, 100, 30);
			btnMoveBack.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent arg0) {
					car.sendCommand().moveBack();
				}
			});
			this.add(btnMoveBack);
		}
		{
			btnMoveLeft = new JButton();
			btnMoveLeft.setText("left");
			btnMoveLeft.setBounds(300, 120, 100, 30);
			btnMoveLeft.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent arg0) {
					car.sendCommand().moveLeft();
				}
			});
			this.add(btnMoveLeft);
		}
		{
			btnMoveRight = new JButton();
			btnMoveRight.setText("right");
			btnMoveRight.setBounds(500, 120, 100, 30);
			btnMoveRight.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent arg0) {
					car.sendCommand().moveRight();
				}
			});
			this.add(btnMoveRight);
		}
		{
			btnTurnLeft = new JButton();
			btnTurnLeft.setText("turn left");
			btnTurnLeft.setBounds(300, 160, 100, 30);
			btnTurnLeft.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent arg0) {
					car.sendCommand().turnLeft();
				}
			});
			this.add(btnTurnLeft);
		}
		{
			btnTurnRight = new JButton();
			btnTurnRight.setText("turn right");
			btnTurnRight.setBounds(500, 160, 100, 30);
			btnTurnRight.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent arg0) {
					car.sendCommand().turnRight();
				}
			});
			this.add(btnTurnRight);
		}
		btns = new ArrayList<JButton>();
		btns.add(btnMoveBack);
		btns.add(btnMoveForward);
		btns.add(btnMoveLeft);
		btns.add(btnMoveRight);
		btns.add(btnTurnLeft);
		btns.add(btnTurnRight);
		setBtnEnable(false);
	}
	private void setBtnEnable(Boolean tag)
	{
		for(JButton temp : btns)
		{
			temp.setEnabled(tag);
		}
	}
}
